package org.jplearning.io;

import java.util.LinkedList;
import java.util.List;

import org.jplearning.model.text.HiraganaString;
import org.jplearning.model.text.IText;
import org.jplearning.model.text.KanjiString;
import org.jplearning.model.text.KatakanaString;
import org.jplearning.model.text.RomanjiString;

public class DictionaryBuilder {
	private static final String HIRAGANA = "hiragana";
	private static final String KATAKANA = "katakana";
	private static final String KANJI = "kanji";
	
	public static final String EL_DICCIONARIO = "dictionary";
	public static final String EL_MEANING = "meaning";
	
	private Dictionary dictionary;
	
	private WordEntry curEntry;
	private IText curWord;
	private WordEntryMeaning curMeaning;
	private WordEntryExample curExample;
	private List<String> curTags;
	
	public Dictionary getDictionary(){
		return dictionary;
	}
	
	public void startEntry(String id){
		curEntry = new WordEntry(id);
	}
	
	public void endEntry(){
		dictionary.addEntries(curEntry);
		curEntry = null;
	}
	
	public void startWord(String alphabet){
		if(HIRAGANA.equals(alphabet)){
			curWord = new HiraganaString();
		}
		else if(KATAKANA.equals(alphabet)){
			curWord = new KatakanaString();
		}
		else if(KANJI.equals(alphabet)){
			curWord = new KanjiString();
		}
		else{
			curWord = new RomanjiString();
		}
	}
	
	public void endWord(){
		curEntry.addWord(curWord);
	}
	
	public void startMeaning(){
		curMeaning = new WordEntryMeaning();
	}
	
	public void endMeaning(){
		curEntry.addMeaning(curMeaning);
	}
	
	public void startTags(){
		curTags = new LinkedList<String>();
	}
	
	public void endTags(String elemento){
		if(EL_DICCIONARIO.equals(elemento)){
			dictionary.setTags(curTags);
		}
		else if(EL_MEANING.equals(elemento)){
			curMeaning.setTags(curTags);
		}
	}
	
	public void startExample(){
		curExample = new WordEntryExample();
	}
	
	public void endExample(){
		curMeaning.addExample(curExample);
	}
}
